import scrapy
from book_spider.items import BookItem


class BooksSpider(scrapy.Spider):
    name = "books"
    allowed_domains = ["books.toscrape.com"]
    start_urls = ["http://books.toscrape.com/"]

    def parse(self, response):
        # 提取页面中的所有书籍链接
        book_links = response.css('article.product_pod h3 a::attr(href)').getall()
        
        # 跟踪每个书籍链接
        for link in book_links:
            yield response.follow(link, self.parse_book)
        
        # 查找并跟踪下一页链接
        next_page = response.css('li.next a::attr(href)').get()
        if next_page:
            yield response.follow(next_page, self.parse)
    
    def parse_book(self, response):
        # 创建一个新的BookItem实例
        item = BookItem()
        
        # 提取书籍信息
        item['title'] = response.css('div.product_main h1::text').get()
        item['price'] = response.css('div.product_main p.price_color::text').get()
        item['availability'] = response.css('div.product_main p.instock.availability::text').getall()[-1].strip()
        item['rating'] = response.css('div.product_main p.star-rating::attr(class)').get().split()[-1]
        item['image_url'] = response.urljoin(response.css('div.item.active img::attr(src)').get())
        item['product_url'] = response.url
        
        yield item